/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package systole.view.crud.profession;


import java.awt.Frame;
import java.util.Calendar;
import javax.swing.JDialog;
import systole.domain.persons.profession.Profession;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;

/**
 *
 * @author jmj
 */
public class ControllerProfessionEdition extends ControllerEntityEdition {


    /**
     *
     */
    public ControllerProfessionEdition() {
        super();
        this.entityName = "Profesión";
        this.newDescription = "Nueva";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        Profession professionToLoad = (Profession) this.curretnEntity;
        JDialogProfession formToLoad = (JDialogProfession) this.editForm;

        professionToLoad.setName(formToLoad.getjEdtName().getText());
        professionToLoad.setDescription(formToLoad.getjTxtDescription().getText());
        professionToLoad.setLastUpdate(Calendar.getInstance());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogProfession formToLoad = (JDialogProfession) this.editForm;
        Profession currentProfession = (Profession) this.curretnEntity;
        formToLoad.getjEdtName().setText(currentProfession.getName());
        formToLoad.getjTxtDescription().setText(currentProfession.getDescription());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        JDialogProfession formToValidate = (JDialogProfession) this.editForm;
        Profession profession = (Profession) this.curretnEntity;

        if ((formToValidate.getjEdtName().getText() == null) || (formToValidate.getjEdtName().getText().isEmpty())) {
            return "Debe ingresar el nombre de la Profesión";
        }

        try {
            if (this.facadeDB.getProfessionBroker().existProfession(formToValidate.getjEdtName().getText(), profession.getId())) {
                return "Ya existe una profesión con el nombre ingresado";
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }

        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
            if (this.isEditing()) {
                this.facadeDB.getProfessionBroker().update((Profession) this.curretnEntity);
            } else {
                this.facadeDB.getProfessionBroker().insert((Profession) this.curretnEntity);
            }
    }

    @Override
    protected void loadIconOnForm() {
       this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/professions/diploma16.png"));
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new Profession();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogProfession(parent, this);
    }

}
